<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/11/8
 * Time: 16:22
 */
namespace app\admin\controller;
use app\admin\model\AuthGroup;
use app\admin\model\AuthRules;

class Role extends Base{
    public function index(){

        $keyword = input('param.title');
        $where = [];
        if(!empty($keyword)){
            $where['title'] = ['like',"%{$keyword}%"];
        }
        $authgroup = AuthGroup::where($where)->paginate(10,false,[
            'query'=>[
                'title'=>$keyword
            ]
        ]);
        $this->assign('auth_group',$authgroup);
        $this->assign('title',$keyword);

        return $this->fetch();
    }

    //添加角色
    public function add(){
        if(request()->isPost()){
            $data = input('post.');
            $Authgroup = new AuthGroup();
            return $Authgroup->add($data);
        }else{
            return $this->fetch();
        }
    }

    //修改
    public function edit(){
        if(request()->isPost()){
            $data = input('post.');
            $AuthGroup = new AuthGroup();
            return $AuthGroup->edit($data);
        }else{
            $id = input('param.id');
            $data = AuthGroup::get($id);
            $this->assign('data',$data);
            return $this->fetch();
        }
    }

    //获取权限
    public function get_rules(){
        $AuthRules = AuthRules::get_all();
        $role_id = input('id');
        $role = AuthGroup::get($role_id);
        $rules = explode(',',$role->rules);
        $data = [];
        if($AuthRules){
            foreach ($AuthRules as $k=>$v){
                $data[] = [
                  'id'=>$v['id'],
                    "pId"=>$v['pid'],
                    "name"=>$v['title'] ,
                    "checked"=>in_array($v['id'],$rules)?1:0
                ];
            }
        }
        return json(['code'=>200,'data'=>$data,'msg'=>'ok']);
    }
    //设置权限
    public function set_rules(){
        $data = input('param.');
        $authgroup = AuthGroup::get($data['id']);
        $re= $authgroup->save([
            'rules'=>$data['rule']
        ]);
        if($re)  return json(['code'=>200,'data'=>'','msg'=>'分配成功']);
        return json(['code'=>1,'data'=>'','msg'=>'分配失败']);
        }

    //修改状态
    public function set_status(){
        if(!request()->isPost()) return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        $status = input('param.status');
        $id = input('param.id');
        $re = AuthGroup::where('id',$id)->update([
            'status'=>$status
        ]);
        if(!$re)  return json(['code'=>1,'data'=>'','msg'=>'修改失败']);
        return json(['code'=>200,'data'=>'','msg'=>'修改成功']);
    }
    //删除
    public function del(){
        if(!request()->isPost()) return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        $id = input('param.');
        if(is_array($id)){
            foreach ($id as $k=>$v){
                AuthGroup::destroy($v);
            }
        }
        return json(['code'=>200,'data'=>$id,'msg'=>'ok']);
    }
}